package com.example.kliven.because.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.example.kliven.because.R;
import com.example.kliven.because.Singleton.VolleySingleton;
import com.example.kliven.because.bean.NearbyBean;

import java.util.List;

/**
 * Created by Kliven on 16/1/12.
 */
public class NearbyAdapter extends BaseAdapter {

    private Context context;
    private List<NearbyBean> nearbyBeans;
    private RequestQueue queue;

    public NearbyAdapter(Context context, List<NearbyBean> nearbyBeans) {
        this.context = context;
        this.nearbyBeans = nearbyBeans;
        VolleySingleton.getInstance();
    }

    @Override
    public int getCount() {
        return nearbyBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return nearbyBeans;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        NearbyViewHodler nearbyViewHodler;
        if(view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_lv_nearby,viewGroup,false);
            nearbyViewHodler = new NearbyViewHodler(view);
            view.setTag(nearbyViewHodler);
        } else {
            nearbyViewHodler = (NearbyViewHodler) view.getTag();
        }
        nearbyViewHodler.tv_shop_name.setText(nearbyBeans.get(i).getShop_name());
        nearbyViewHodler.tv_arg_spend.setText(nearbyBeans.get(i).getArg_spend());
        nearbyViewHodler.tv_distance.setText(nearbyBeans.get(i).getDistance());
        nearbyViewHodler.tv_sharecount.setText(nearbyBeans.get(i).getSharecount());
        nearbyViewHodler.share_url_and_id.setImageURI(Uri.parse(nearbyBeans.get(i).getShare_url_and_id()));

//        queue = Volley.newRequestQueue(context);
//        MemoryCache cache = new MemoryCache();
        // 4. 初始化ImageLoager
//        ImageLoader imageLoader = new ImageLoader(queue, cache);

        nearbyViewHodler.share_url_and_id.setDefaultImageResId(R.mipmap.error_img);
        nearbyViewHodler.share_url_and_id.setErrorImageResId(R.mipmap.error_img);
        nearbyViewHodler.share_url_and_id.setImageUrl(nearbyBeans.get(i).getShare_url_and_id(),VolleySingleton.getImageLoader());
        return view;
    }

    class NearbyViewHodler{
        TextView tv_shop_name;
        TextView tv_distance;
        TextView tv_arg_spend;
        TextView tv_sharecount;
        NetworkImageView share_url_and_id;
        public NearbyViewHodler(View view){
            tv_shop_name = (TextView) view.findViewById(R.id.shop_name);
            tv_distance = (TextView) view.findViewById(R.id.distance);
            tv_arg_spend = (TextView) view.findViewById(R.id.arg_spend);
            tv_sharecount = (TextView) view.findViewById(R.id.sharecount);
            share_url_and_id = (NetworkImageView) view.findViewById(R.id.share_url_and_id);
        }
    }
}
